<?php


namespace app\common;

use app\common\model\CountAdv;
use think\db\Query;
use think\Validate;

/**
 * 数据统计
 * Class RootContService
 * @package app\common
 */
class RootCountService
{
    /**
     * 引入trait类
     */
    use RootAuth, RootEncrypt;

    /**
     * 异常提示
     * @param string $message
     * @param int $code
     * @throws RootException
     */
    protected function errorException(string $message, int $code = 1)
    {
        throw new RootException($message, $code);
    }

    /**
     * 分页
     * @param Query $query
     * @return array
     * @throws \think\db\exception\DbException
     */
    protected function page(Query $query, array $append = [])
    {
        return $query->paginate(request()->param('limit') ?: 10)->append($append)->toArray();
    }

    /**
     * 参数校验
     * @param Validate $validate
     * @param string $scene
     * @param array $params
     * @throws RootException
     */
    protected function validate(Validate $validate, string $scene, array $params = [])
    {
        $validate->scene($scene)->check($params ?: request()->param()) || $this->errorException($validate->getError());
    }


    protected function getData($model, array $params)
    {
        $where = [];
        // 日期筛选
        empty($params['writeDate']) || $where[] = ['record_date', 'between', betweenDate($params['writeDate'])];

        return $model::where($where)->order('record_date', 'desc')->paginate($params['limit'] ?? 10)->toArray();
    }
}